using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GHMC
{
	public partial class UserJob : Form
	{
		ListViewEx.ListViewEx lView;
		List<User> m_List;

		public UserJob(ref ListViewEx.ListViewEx lstView, ref List<User> list)
		{
			InitializeComponent();
			lView = lstView;
			m_List = list;
		}

		public void getUserInfo(string auth, Mgsi.UdMgsi ud, Mgsi.User user)
		{
			Mgsi.JobFilter jf = new Mgsi.JobFilter();
			jf.creator_gid_match = new string[] {user.user_gid};
			
			Mgsi.JobArrayRange jar = ud.getJobs(auth, jf, "job_id desc", 0, -1);
	
			foreach(Mgsi.Job j in jar.records)
			{
				ListViewItem l = new ListViewItem(j.job_id.ToString());
				l.SubItems.Add(j.description);

				if (j.state_id == 1)
				{
					l.SubItems.Add("Incomplete");
				}
				else if (j.state_id == 2)
				{
					l.SubItems.Add("Suspended");
				}
				else if (j.state_id == 4)
				{
					l.SubItems.Add("Failed");
				}
				else if (j.state_id == 3)
				{
					l.SubItems.Add("Completed");
				}

				lstUser.Items.Add(l);
			}
			
			if (lstUser.Items.Count == 0)
			{
				MessageBox.Show("Error in retrieving user '" + user.user_name + "' jobs - please try again");
				return;
			}

			this.ShowDialog();
			return;
		}

        public void getUserInfo(string auth, Mgsi.UdMgsi ud, Mgsi.Application app)
        {
            Mgsi.JobFilter jf = new Mgsi.JobFilter();
            jf.application_gid_match = new string[] { app.application_gid };

            Mgsi.JobArrayRange jar = ud.getJobs(auth, jf, "job_id desc", 0, -1);

            foreach (Mgsi.Job j in jar.records)
            {
                ListViewItem l = new ListViewItem(j.job_id.ToString());
                l.SubItems.Add(j.description);

                if (j.state_id == 1)
                {
                    l.SubItems.Add("Incomplete");
                }
                else if (j.state_id == 2)
                {
                    l.SubItems.Add("Suspended");
                }
                else if (j.state_id == 4)
                {
                    l.SubItems.Add("Failed");
                }
                else if (j.state_id == 3)
                {
                    l.SubItems.Add("Completed");
                }

                lstUser.Items.Add(l);
            }

            if (lstUser.Items.Count == 0)
            {
                MessageBox.Show("Error in retrieving application '" + app.application_name + "' jobs - please try again");
                return;
            }

            this.ShowDialog();
            return;
        }

        public void getUserInfo(string auth, Mgsi.UdMgsi ud, string[] userGids, Mgsi.Application app)
        {
            Mgsi.JobFilter jf = new Mgsi.JobFilter();
            jf.creator_gid_match = userGids;
            jf.application_gid_match = new string[] { app.application_gid };

			Mgsi.JobArrayRange jar = null;
			try
			{
				jar = ud.getJobs(auth, jf, "job_id desc", 0, -1);
			}
			catch (Exception jare)
			{
				throw new Exception("Unable to retrieve jobs in UserJob class - contact administrator");
			}

            foreach (Mgsi.Job j in jar.records)
            {
                ListViewItem l = new ListViewItem(j.job_id.ToString());
                l.SubItems.Add(j.description);

                if (j.state_id == 1)
                {
                    l.SubItems.Add("Incomplete");
                }
                else if (j.state_id == 2)
                {
                    l.SubItems.Add("Suspended");
                }
                else if (j.state_id == 4)
                {
                    l.SubItems.Add("Failed");
                }
                else if (j.state_id == 3)
                {
                    l.SubItems.Add("Completed");
                }

                lstUser.Items.Add(l);
            }

            if (lstUser.Items.Count == 0)
            {
                MessageBox.Show("Error in retrieving application '" + app.application_name + "' jobs - please try again");
                return;
            }

            this.ShowDialog();
            return;
        }

		private void addUserInfo()
		{
			// This should be moved to the button_click action
			try
			{
				if (lstUser.SelectedIndices.Count != 0)
				{
					foreach (int index in lstUser.SelectedIndices)
					{
						ListViewItem temp = (ListViewItem)lstUser.Items[index].Clone();
						lView.Items.Add(temp);
						User u = new User(int.Parse(lstUser.Items[index].Text), lstUser.Items[index].SubItems[1].Text, lstUser.Items[index].SubItems[2].Text);
						m_List.Add(u);
					}
				}
				else
				{
					throw new Exception("Please select a job to add");
				}
			}
			catch (Exception exss)
			{
				MessageBox.Show("Error: " + exss.Message);
			}
		}

		private void butAdd_Click(object sender, EventArgs e)
		{
			addUserInfo();
			this.Close();
		}

		private void butCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}